package com.lanren.familybookapp.ui.me.view;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.hjq.toast.ToastUtils;
import com.lanren.common.helper.InputTextHelper;
import com.lanren.common.interfaces.DialogClickListener;
import com.lanren.common.utils.EditTextUtils;
import com.lanren.common.utils.StringUtils;
import com.lanren.common.utils.Utils;
import com.lanren.common.utils.event.C;
import com.lanren.common.widget.ClearEditText;
import com.lanren.common.widget.CountdownView;
import com.lanren.common.widget.TitleBarView;
import com.lanren.common.widget.statusbar.ImmersionBar;
import com.lanren.easyeventbus.Event;
import com.lanren.easyeventbus.EventBusUtil;
import com.lanren.familybookapp.R;
import com.lanren.familybookapp.base.BaseUiActivity;
import com.lanren.familybookapp.ui.me.interfaces.IEmailModel;
import com.lanren.familybookapp.ui.me.presenter.EmailPresenter;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @ClassName BookEmailActivity
 * @Description TODO
 * @Author chongheng.wang
 * @Date 2019/10/11 19:11
 * @Version 1.0
 */
public class BookEmailActivity extends BaseUiActivity<EmailPresenter> implements IEmailModel.IEmailView {
    @BindView(R.id.title_view)
    TitleBarView titleView;
    @BindView(R.id.img_email)
    ImageView imgEmail;
    @BindView(R.id.et_email)
    ClearEditText etEmail;
    @BindView(R.id.img_security)
    ImageView imgSecurity;
    @BindView(R.id.et_email_code)
    ClearEditText etEmailCode;
    @BindView(R.id.cv_email_countdown)
    CountdownView cvEmailCountdown;
    @BindView(R.id.btn_email_commit)
    Button btnEmailCommit;

    @Override
    public EmailPresenter createPresenter() {
        return new EmailPresenter();
    }

    @Override
    protected void setStatusBar() {
        mImmersionBar = ImmersionBar.with(this)
                .statusBarDarkFont(true, 0.2f)
                .init();
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_email;
    }

    @Override
    public void doCreate(Bundle savedInstanceState) {
        initView();
        initData();
        initEvent();
    }

    public void initView() {
        EditTextUtils.setEditTextFilter(etEmail);
        EditTextUtils.setEditTextFilter(etEmailCode);
        new InputTextHelper.Builder(this)
                .setMain(btnEmailCommit)
                .addView(etEmail)
                .addView(etEmailCode)
                .build();

    }

    @SuppressLint("SetTextI18n")
    public void initData() {

    }

    public void initEvent() {
        titleView.setBackListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @OnClick({R.id.cv_email_countdown, R.id.btn_email_commit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.cv_email_countdown:
                if (Utils.isFastClick(view)) {
                    if (StringUtils.isEmail(etEmail.getText().toString())) {
                        mPresenter.getEmailCode(etEmail.getText().toString());
                        ToastUtils.show(getString(R.string.common_send_code_succeed));
                    } else {
                        cvEmailCountdown.resetState();
                        ToastUtils.show(getString(R.string.common_email_input_error));
                    }
                }
                break;
            case R.id.btn_email_commit:
                if (Utils.isFastClick(view)) {
                    mPresenter.addEmailCode(etEmail.getText().toString(), etEmailCode.getText().toString());
                }
                break;
        }
    }


    @Override
    public void onSuccessEmail(String msg) {
        showMessageDialog(msg, true, new DialogClickListener() {
            @Override
            public void onCancelClick(View v) {

            }

            @Override
            public void onConfirmClick(View v) {
                EventBusUtil.sendEvent(new Event(C.EventCode.REFRESH_USER_INFO));
                finish();
            }
        });
    }

}
